package com.team.engine.util;

import java.util.List;

import org.hibernate.Query;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.orm.ibatis.SqlMapClientTemplate;

/**
 * 分页查询数据库操作类<br/>
 * 范例：在dao类中使用
 * <pre>
 * 	SubPageOperate< T > sub = new SubPageOperate< T >(pageNum, this.selectCount());
	return sub.selectSubData(this.getSession().createQuery("from "+className));
 * </pre>
 * @author MengLH
 * @data 2010-8-24
 * @param <T>
 */
public class SubPageOperate<T>{

//	private int pageNum;
	private Long totalRowNum;
	private SubPage<T> subPage;
	
	private static final String FROM_LOWER = " from ";
	private static final String FROM_UPPER = " FROM ";
	private static final String LIMIT = " LIMIT ";

	public void setPageNum(String page) {
		int pageNum = page==null||page.trim().length()==0?1:Integer.parseInt(page);
		int tmp = pageNum - 1;
		this.subPage.setThisPage(tmp);
//		this.pageNum = tmp;
	}

	public void setTotalRowNum(String totalRowNum) {
		Long tmp = totalRowNum==null||totalRowNum.length()==0||totalRowNum.equals("null")?-1:Long.valueOf(totalRowNum);
		this.totalRowNum = tmp;
		this.subPage.setTotalRowNum(tmp);
	}

	public Long getTotalRowNum() {
		return totalRowNum;
	}

	public SubPageOperate(){
		this.subPage = new SubPage<T>();
	}
	
	public SubPageOperate(String page){
		this.subPage = new SubPage<T>();
		this.setPageNum(page);
	}
	
	public SubPageOperate(String page, String totalRowNum){
		this.subPage = new SubPage<T>();
		this.setTotalRowNum(totalRowNum);
		this.setPageNum(page);
	}
	
	public SubPage<T> selectSubData(Query q){
		if(q == null)
			throw new NullPointerException("Query cannot be null");

		q.setFirstResult(this.subPage.getStartRowNum());
		q.setMaxResults(subPage.getPageScollSize());
		subPage.setResultList(q.list());
		return subPage;
	}
	
	public SubPage<T> selectSubData(SqlMapClientTemplate sqlMapClient) {

		return null;
	}
	
	public SubPage<T> selectSubData(SqlMapClientTemplate sqlMapClient, String sql) {

		return null;
	}

	public SubPage<T> selectSubData(JdbcTemplate jdbcTemplate) {

		return null;
	}
	
	public SubPage<T> selectSubData(JdbcTemplate jdbcTemplate, String sql, RowMapper<T> rowMapper) {
		if(jdbcTemplate == null)
			throw new NullPointerException("JdbcTemplate cannot be null");
		
		if(this.getTotalRowNum() == -1){
			StringBuilder strbu = new StringBuilder(sql);
			String firstColumn  = strbu.substring(6, strbu.indexOf(","));
			
			int end = sql.contains(FROM_LOWER)?strbu.indexOf(FROM_LOWER):strbu.indexOf(FROM_UPPER);
			String tmp = strbu.replace(6, end, " COUNT(DISTINCT "+firstColumn+") ").toString();
			tmp = tmp.replaceAll("(?i)(group by).*$", "");
			
			String longStr = String.valueOf(jdbcTemplate.queryForLong(tmp));
			this.setTotalRowNum(longStr);
		}
		
		String string = sql + LIMIT + this.subPage.getStartRowNum() + "," + subPage.getPageScollSize();
		List<T> query = jdbcTemplate.query(string, rowMapper);
		this.subPage.setResultList(query);
		return subPage;
	}
	
}
